package net.quanter.demo.junit5.basic.rate;

import lombok.Builder;
import lombok.Data;

/***
 * created on 2021-01-19
 * @author 王老实
 *
 */
public class RateCalculator {

    public static RateCalculaResult caleYearRate(RateCalculaParam param) {

        double monthRate = param.basicRateOfYear / 12;
        RateCalculaResult result = RateCalculaResult.builder().account(param.loanMoney).build();
        result.account -= param.charge;
        for (int i = 1; i <= param.month; i++) {
            if (Repayment.EqualInterest == param.repayment) {
                double oldAccount = result.account;
                double repeyMoney = param.repayMoney / param.month;
                double profitMoney = oldAccount * monthRate;
                result.profit += profitMoney;
                result.account = result.account + profitMoney - param.repayMoney / param.month;
                System.out.println(String.format("%d ---- %.2f  + %.2f - %.2f = %.2f", i, oldAccount, profitMoney, repeyMoney, result.account));
            }
        }
        result.rate = result.account / param.loanMoney / (param.month / 12);
        return result;
    }

    @Data
    @Builder
    public static final class RateCalculaParam {
        double basicRateOfYear;
        Repayment repayment;
        double loanMoney;
        double charge;
        double repayMoney;
        int month;
    }

    @Data
    @Builder
    public static final class RateCalculaResult {
        double account;
        double profit;
        double rate;

        public String toString() {
            return String.format("%.2f,%.2f,%.2f%%", account, profit, -rate * 100);
        }

    }

    public enum Repayment {
        EqualPrincipal,
        EqualInterest,
        FirstInterest,
        LastPrincipal;

    }
}
